package cn.rongdao.jrkabao.ui.login.uicomponent;

import android.content.Context;
import android.text.InputFilter;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;

/**
 * Created by jack on 2018/4/20
 */

public class LoginInputUIComponent extends RelativeLayout {
    public LoginInputUIComponent(Context context) {
        super(context);
    }

    public LoginInputUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LoginInputUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private EditText etInput;
    private TextView tvSendMsg;
    private TextView tvDelete;

    private Observable<CharSequence> inputObservable;
    private CompositeDisposable compositeDisposable;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        etInput = findViewById(R.id.lli_et_input);
        tvSendMsg = findViewById(R.id.lli_tv_send_msg);
        tvDelete = findViewById(R.id.lli_tv_delete);

        compositeDisposable = new CompositeDisposable();
        inputObservable = RxTextView.textChanges(etInput);
    }

    public Observable<CharSequence> getInputObservable() {
        return inputObservable;
    }

    public void setSendMsgEnable(boolean isEnable) {
        tvSendMsg.setEnabled(isEnable);
    }

    public void setSendMsgContent(String content) {
        tvSendMsg.setText(content);
    }

    public void setSendMsgOnClickListener(OnClickListener onClickListener) {
        tvSendMsg.setVisibility(View.VISIBLE);
        tvSendMsg.setOnClickListener(onClickListener);
    }

    public String getInputContent() {
        return etInput.getText().toString().trim();
    }

    public void setInputType(int inputType) {
        etInput.setInputType(inputType);
    }

    public void setInputMaxLength(int maxLength) {
        etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
    }

    public void setHint(CharSequence hint) {
        etInput.setHint(hint);
    }

    public void initDelete() {
        compositeDisposable.add(inputObservable.subscribe(new Consumer<CharSequence>() {
            @Override
            public void accept(CharSequence charSequence) throws Exception {
                tvDelete.setVisibility(FRString.isEmpty(FRString.valueOf(charSequence)) ? View.GONE : View.VISIBLE);
            }
        }));
        tvDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                etInput.setText("");
            }
        });
    }

    public void setPassword(){
//        etInput.setTransformationMethod(PasswordTransformationMethod.getInstance());
        etInput.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    public void onDestroy() {
        if (null != compositeDisposable) {
            compositeDisposable.dispose();
            compositeDisposable = null;
        }
    }
}